package yhao.infra.web.common.enumsmap;


import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * 对应 HTML 页面上的 select option 对象
 *
 * @author wuweipeng
 * @author liwan
 * @date 2021/7/20 9:16
 *
 */

public class SelectOption {

    /**
     * 默认映射枚举的 alias的值
     */
    private Object name;

    /**
     * 默认映射枚举的 value的值 如果value为空 则默认取枚举的name名称
     */
    private Object value;

    /**
     * 枚举额外的描述字段
     */
    private Map<String, Object> extAttr = new HashMap<>();

    public SelectOption(Object name, Object value) {
        this.name = name;
        this.value = value;
    }

    public boolean hasFull() {
        return this.name != null && this.value != null;
    }

    public void addAttr(String key, Object value) {
        this.extAttr.put(key, value);
    }

    @Override
    public String toString() {
        return Optional.ofNullable(this.name).map(Object::toString).orElse("")
                + " : " +
                Optional.ofNullable(this.value).map(Object::toString).orElse("");
    }

    public String toOption() {
        StringBuilder option = new StringBuilder();
        option.append("<option value='").append(this.getValue())
                .append("'>").append(this.getName()).append("</option>");
        return option.toString();
    }

    public Object getName() {
        return name;
    }

    public void setName(Object name) {
        this.name = name;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public Map<String, Object> getExtAttr() {
        return extAttr;
    }

    public void setExtAttr(Map<String, Object> extAttr) {
        this.extAttr = extAttr;
    }
}
